$().ready(function() {
	validateRule();

	$("#projectIds").val( $("#projectId").val() )
	$("#states").val( $("#state").val() )
	let img = $("#img").val()
	if( img ){
		let str = '' ;
		str += `
					<div class="closeBtn">
						<img class="upImg" src="${img}" />
						<span class="iconclose close2" ><img src="../../../img/close.png"></span>
					</div>
				  `;

		$(".listImg").html(str)
	}

});

$.validator.setDefaults({
	submitHandler : function() {
		update();
	}
});
function update() {
	$.ajax({
		cache : true,
		type : "POST",
		url : "/jzweb/zykManagementResources/update",
		data : $('#signupForm').serialize(),// 你的formid
		async : false,
		error : function(request) {
			parent.layer.alert("Connection error");
		},
		success : function(data) {
			if (data.code == 0) {
				parent.layer.msg("操作成功");
				parent.reLoad();
				var index = parent.layer.getFrameIndex(window.name); // 获取窗口索引
				parent.layer.close(index);

			} else {
				parent.layer.alert(data.msg)
			}

		}
	});

}
function validateRule() {
	var icon = "<i class='fa fa-times-circle'></i> ";
	$("#signupForm").validate({
		rules : {
			name : {
				required : true
			}
		},
		messages : {
			name : {
				required : icon + "请输入资源名"
			}
		}
	})
}

$.ajax({
	cache : true,
	type : "get",
	url : "/jzweb/jazSysProject/list",
	data : {
		current:1,
		size:1000
	},// 你的formid
	async : false,
	error : function(request) {
		parent.layer.alert("Connection error");
	},
	success : function(data) {
		if (data.code == 0) {
			let str = '';
			data.data.records.map(function (val,k){
				if( k == 0 ){ str += `<option value="">请选择</option>` }
				str += `<option value="${val.id}">${val.name}</option>`
			})
			$("#projectIds").html(str)
			// parent.layer.msg("操作成功");
			// parent.reLoad();
			// var index = parent.layer.getFrameIndex(window.name); // 获取窗口索引
			// parent.layer.close(index);

		} else {
			parent.layer.alert(data.msg)
		}

	}
});

layui.use(['upload', 'layer'], function() {
	var $ = layui.jquery
		, upload = layui.upload
		, layer = layui.layer;

	//图片上传
	var uploadInst = upload.render({
		elem: '#test1'
		,url: '/common/sysFile/upload' // 实际使用时改成您自己的上传接口即可。
		,multiple: true
		,before: function(obj){
			layer.load();
		}
		,done: function(res){
			if( res.code == 0 ){
				layer.closeAll();
				let str = '' ;
				str += `
					<div class="closeBtn">
						<img class="upImg" src="${res.data}" />
						<span class="iconclose close2" ><img src="../../../img/close.png"></span>
					</div>
				  `;

				$(".listImg").html(str)
				$("#img").val(res.data)

			}
		}
		,error:function (res){
			layer.closeAll();
			layer.msg(res.msg)
		}
	});

})

$(document).on('click', '.close2', function() {
	removeContent( this , '.closeBtn' , '.listImg' )
});

function removeContent(that, parent, element) {
	let str = '';
	$("#img").val('')
	$(that).parents(parent).remove();
	$(element).html(str);
}